<template>
  <a-modal
    :title="title"
    :visible="visible"
    :width="800"
    :mask-closable="false"
    :confirmLoading="loading"
    @cancel="hide"
    @ok="ok"
  >
    <a-form-model
      ref="form"
      :model="form"
      :rules="rules">
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span12">
          <a-form-model-item label="车牌号码">
            <a-input disabled v-model="form.number" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
        <a-col :span="formLayout.span12">
          <a-form-model-item label="车辆颜色">
            <a-input v-model="form.color" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span24">
          <a-form-model-item label="车位照片">
            <ImageUploader v-model="form.images" :limitNum="4" />
          </a-form-model-item>
        </a-col>
      </a-row>
      <a-row :gutter="formLayout.gutter">
        <a-col :span="formLayout.span24">
          <a-form-model-item label="备注">
            <a-input v-model="form.note" type="textarea" :rows="4" placeholder="请输入" />
          </a-form-model-item>
        </a-col>
      </a-row>
    </a-form-model>
  </a-modal>
</template>

<script>
import ImageUploader from '@/components/ImageUploader/ImageUploader.vue'

let originForm = null

export default {
  components: { ImageUploader },
  data () {
    return {
      visible: false,
      loading: false,
      title: '',
      form: {
        number: '',
        color: '',
        images: [],
        note: ''
      },
      rules: {},
      cb: null
    }
  },
  created () {
    originForm = this.deepClone(this.form)
  },
  methods: {
    open (title, form, cb) {
      this.title = title
      this.cb = cb
      if (form) {
        for (const key in this.form) {
          this.form[key] = form[key]
        }
      }
      this.visible = true
    },
    hide (close = true) {
      if (close) {
        this.form = this.deepClone(originForm)
        this.loading = false
        this.visible = false
      } else {
        this.loading = false
      }
    },
    ok () {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.loading = true
          this.cb && this.cb(this.form, this.hide)
        }
      })
    }
  }
}
</script>
